package com.boho.system.dao;


             
/**
 * 
 *
 * @File name:  TSysRoleDao.java 
 * @Create on:  2010-05-10 11:27:234
 * @Author   :  陈伟
 *
 * @ChangeList
 * ---------------------------------------------------
 * Date         Editor              ChangeReasons
 *
 */
           

            
import java.sql.Connection;
import java.util.List;

import cn.boho.framework.po.CQuery;
import cn.boho.framework.po.CQueryFactoryTool;
import cn.boho.framework.po.ComboPager;
import cn.boho.framework.po.DAO;
import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.po.Pager;

import com.boho.common.Constant;
            
/**
 * 【系统角色信息:T_SYS_ROLE】的Dao操作类
 * 
 */
public class TSysRoleDao extends DAO {

	/**
	* 根据角色名称【角色名称:ROLE_NAME|】查询角色是否存在
	* @param con
	* @param ROLE_NAME 角色名称
	* @return
	* @throws Exception
	* @author 陈伟
	*
	*/
	@SuppressWarnings("unchecked")
	public static DynaBeanMap getRoleByRoleName(Connection con, String rolename) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("select ROLE_ID from T_ROLE where lower(ROLE_NAME) = ?");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		query.setString(1, rolename.toLowerCase());
		query.setCommand(sql.toString());
		logger.debug("TSysRoleDao.getRoleByRoleName() SQL->"+query.getCommand());
		return query.getDynaBeanMap("T_ROLE", con);
	}
	
	/**
	* 分页查询所有【系统角色信息:T_SYS_ROLE】信息
	* @param con
	* @param pager
	* @return
	* @throws Exception
	* @author 陈伟
	*
	*/
	public static ComboPager queryTSysRoleByPager(Connection con,Pager pager, String rolename)throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("select ROLE_ID,ROLE_NAME,ROLE_DESC from T_ROLE where 1 = 1");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		if(rolename != null){
			sql.append(" and lower(ROLE_NAME) like ?");
			query.setString(1, "%"+rolename.toLowerCase()+"%");
		}
		sql.append(" and ROLE_ID <> ");
		sql.append(Constant.ADMIN_ROLE);
		sql.append(" order by ROLE_ID asc");
		query.setCommand(sql.toString());
		logger.debug("TSysRoleDao.queryTSysRoleByPager() SQL->"+query.getCommand());
		return query.getDynaBeanMapComboPager("T_ROLE", pager,con);
	}
	/**
	* 根据主键【角色ID:PK_ROLE|】获得信息
	* @param con
	* @param pkRole 角色ID
	* @return
	* @throws Exception
	* @author 陈伟
	*
	*/
	@SuppressWarnings("unchecked")
	public static DynaBeanMap getTSysRoleByPk(Connection con,Integer roleID)throws Exception{
		DynaBeanMap dynaBeanMap = null;
		StringBuffer sql = new StringBuffer();
		sql.append("select ROLE_NAME,ROLE_DESC from T_ROLE where ROLE_ID=?");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		query.setCommand(sql.toString());
		query.setInt(1,roleID);
		logger.debug("TSysRoleDao.getTSysRoleByPk() SQL->"+query.getCommand());
		dynaBeanMap = query.getDynaBeanMap("T_ROLE", con);
		return dynaBeanMap;
	}

	/**
	 * 
	 * @param con
	 * @param rolename
	 * @param roleid
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static DynaBeanMap getRoleByRoleNameByUpdate(Connection con, String rolename, Integer roleid) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("select ROLE_ID from T_ROLE where ROLE_NAME = ? and ROLE_ID <> ?");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		query.setString(1, rolename);
		query.setLong(2, roleid);
		query.setCommand(sql.toString());
		logger.debug("TSysRoleDao.getRoleByRoleNameByUpdate() SQL->"+query.getCommand());
		return query.getDynaBeanMap("T_ROLE", con);
	}
	
	/**
	* 查询所有【系统角色信息:T_SYS_ROLE】信息
	* @param con
	* @return
	* @throws Exception
	* @author 陈伟
	*
	*/
	@SuppressWarnings("unchecked")
	public static List queryTSysRole(Connection con)throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("select ROLE_ID,ROLE_NAME from T_ROLE where ROLE_ID <> ?");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		query.setLong(1, Constant.ADMIN_ROLE);
		query.setCommand(sql.toString());
		logger.debug("TSysRoleDao.queryTSysRole() SQL->"+query.getCommand());
		return query.getDynaBeanMapList("T_ROLE", con);
	}
	
	/**
	* 根据角色ID查询所有【系统角色功能:T_SYS_ROLE_FUNCTION】信息
	* @param con
	* @return
	* @throws Exception
	* @author 陈伟
	*
	*/
	@SuppressWarnings("unchecked")
	public static List queryTSysRoleFunctionByPKRole(Connection con, Integer pkRole)throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("select f.FUNCTION_ID, FUNCTION_NAME, FUNCTION_DESC,PARENT_FUNCTION, FUNCTION_LEVEL,URL ");
		sql.append("from t_role_function rf, t_function f where rf.FUNCTION_ID = f.FUNCTION_ID ");
		sql.append("and rf.ROLE_ID = ? order by FUNCTION_ID asc");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		query.setInt(3, pkRole);
		query.setCommand(sql.toString());
		logger.debug("TSysRoleDao.queryTSysRoleFunctionByPKRole() SQL->"+query.getCommand());
		return query.getDynaBeanMapList("T_ROLE_FUNCTION", con);
	}
}
